// src/api/auth.js
import axios from 'axios';
import { useCacheStore } from '@/store/cache/cache';

const API_URL = 'http://localhost:9001'; // 替换为你的后端 API 地址

export const login = async (mobile, password) => {
    console.log("开始登录")
    const cacheStore = useCacheStore();
    try {
        const response = await axios.post(`${API_URL}/user/login`, {
            mobile,
            password,
        });
        console.log("登录成功！返回的数据为："+JSON.stringify(response.data.data) )
        cacheStore.setCachedData(response.data.data)
        return response.data; // 返回后端响应的数据
    } catch (error) {
        throw error.response.data; // 抛出错误信息
    }
};

export const register = async (nickName, password, mobile) => {
    console.log("开始注册用户")
    try {
        const response = await axios.post(`${API_URL}/user/register`, {
            nickName,
            password,
            mobile,
        }
        );
        if(response.data.code === "999"){
            throw error.response.desc
        }
        console.log("注册响应的数据为："+ response.data)
        return response.data; // 返回后端响应的数据
    } catch (error) {
        throw error.response.data; // 抛出错误信息
    }
};